package com.sailfish.springbootdemo.service.db1;

import com.sailfish.springbootdemo.dao.db1.MaintenanceConfigDao;
import com.sailfish.springbootdemo.dao.db1.PcbaDeviceDao;
import com.sailfish.springbootdemo.dao.db1.UpkeepDao;
import com.sailfish.springbootdemo.dao.db1.UpkeepHistoryDao;
import com.sailfish.springbootdemo.pojo.db1.MaintenanceConfig;
import com.sailfish.springbootdemo.pojo.db1.PcbaDevice;
import com.sailfish.springbootdemo.pojo.db1.Upkeep;
import com.sailfish.springbootdemo.pojo.db1.UpkeepHistory;
import com.sailfish.springbootdemo.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class TimerServiceImpl implements TimerService {

    @Autowired
    private UpkeepDao upkeepDao;

    @Autowired
    private UpkeepHistoryDao upkeepHistoryDao;

    @Autowired
    private PcbaDeviceDao pcbaDeviceDao;

    @Autowired
    private MaintenanceConfigDao maintenanceConfigDao;

    @Override
    public void judgeRefreshDeviceUpkeepStatus() {
        try {
            int delayDay = 3;
            List<MaintenanceConfig> maintenanceConfigList = maintenanceConfigDao.getMaintenanceConfigByType(3);
            if (maintenanceConfigList.size() > 0) {
                delayDay = maintenanceConfigList.get(0).getUpkeepAdvanceOrDelay();
            }

            List<PcbaDevice> pcbaDeviceList = pcbaDeviceDao.getAllPcbaDevice(null, null, null, null, null, null, null, null, null, null);

            for (PcbaDevice pcbaDevice : pcbaDeviceList) {
                boolean isExistOverDueUpkeepFlag = false;
                String assetNumber = pcbaDevice.getAssetNumber();
                if (assetNumber != null && !"".equals(assetNumber)) {
                    List<Upkeep> upkeepList = upkeepDao.getUpkeepByDeviceId(pcbaDevice.getAssetNumber(), null);
                    if (upkeepList.size() > 0) {
                        // 需要对比判断保养记录
                        for (Upkeep upkeep : upkeepList) {
                            Date firstUpkeepTime = upkeep.getFirstUpkeepTime();
                            Integer upkeepType = upkeep.getUpkeepType();
                            List<String> monthBetweenDate = CommonUtils.getMonthBetweenDate(firstUpkeepTime, new Date());

                            for (int a = 0; a < monthBetweenDate.size(); a++) {
                                List<UpkeepHistory> upkeepHistoryList = upkeepHistoryDao.getUpkeepHistoryByAssetNumberAndDueUpkeepTime(assetNumber, upkeepType, monthBetweenDate.get(a));
                                if (upkeepHistoryList.size() > 0) {
                                    // 存在保养计划的情况下，需要更新原先的保养记录
                                    // 比如
                                    // 1.超过三天的需要改为逾期
                                    // 2.到达下一个节点的需要新增一个记录


                                } else {
                                    // 不存在保养记录[插入表，状态需判断是否超期，超期改5，正常改4]  asset_number,create_time,status,due_upkeep_time,upkeep_type
                                    if (upkeepType == 1) {
                                        Calendar calendar = Calendar.getInstance();
                                        calendar.setTime(firstUpkeepTime);
                                        calendar.add(Calendar.MONTH, a);//增加1个月
                                        Date time = calendar.getTime();

                                        // 判断与当前时间差是否在 delayDay 之间 ，是的话状态4，不是的话状态5
                                        int status = 4;
                                        long daysBetweenTwoDate = CommonUtils.getDaysBetweenTwoDate(time, new Date());
                                        if (daysBetweenTwoDate > delayDay) {
                                            status = 5;
                                            isExistOverDueUpkeepFlag = true;
                                        }

                                        UpkeepHistory addUpkeepHistory = new UpkeepHistory();
                                        addUpkeepHistory.setAssetNumber(assetNumber);
                                        addUpkeepHistory.setCreateTime(new Date());
                                        addUpkeepHistory.setStatus(status);
                                        addUpkeepHistory.setDueUpkeepTime(time);
                                        addUpkeepHistory.setUpkeepType(upkeepType);
                                        upkeepHistoryDao.addUpkeepHistory(addUpkeepHistory);
                                    } else if (upkeepType == 2) {
                                        if (a % 3 == 0) {
                                            Calendar calendar = Calendar.getInstance();
                                            calendar.setTime(firstUpkeepTime);
                                            calendar.add(Calendar.MONTH, a);//增加3个月
                                            Date time = calendar.getTime();

                                            // 判断与当前时间差是否在 delayDay 之间 ，是的话状态4，不是的话状态5
                                            int status = 4;
                                            long daysBetweenTwoDate = CommonUtils.getDaysBetweenTwoDate(time, new Date());
                                            if (daysBetweenTwoDate > delayDay) {
                                                status = 5;
                                                isExistOverDueUpkeepFlag = true;
                                            }

                                            UpkeepHistory addUpkeepHistory = new UpkeepHistory();
                                            addUpkeepHistory.setAssetNumber(assetNumber);
                                            addUpkeepHistory.setCreateTime(new Date());
                                            addUpkeepHistory.setStatus(status);
                                            addUpkeepHistory.setDueUpkeepTime(time);
                                            addUpkeepHistory.setUpkeepType(upkeepType);
                                            upkeepHistoryDao.addUpkeepHistory(addUpkeepHistory);
                                        }
                                    } else if (upkeepType == 3) {
                                        if (a % 6 == 0) {
                                            Calendar calendar = Calendar.getInstance();
                                            calendar.setTime(firstUpkeepTime);
                                            calendar.add(Calendar.MONTH, a);//增加6个月
                                            Date time = calendar.getTime();

                                            // 判断与当前时间差是否在 delayDay 之间 ，是的话状态4，不是的话状态5
                                            int status = 4;
                                            long daysBetweenTwoDate = CommonUtils.getDaysBetweenTwoDate(time, new Date());
                                            if (daysBetweenTwoDate > delayDay) {
                                                status = 5;
                                                isExistOverDueUpkeepFlag = true;
                                            }

                                            UpkeepHistory addUpkeepHistory = new UpkeepHistory();
                                            addUpkeepHistory.setAssetNumber(assetNumber);
                                            addUpkeepHistory.setCreateTime(new Date());
                                            addUpkeepHistory.setStatus(status);
                                            addUpkeepHistory.setDueUpkeepTime(time);
                                            addUpkeepHistory.setUpkeepType(upkeepType);
                                            upkeepHistoryDao.addUpkeepHistory(addUpkeepHistory);
                                        }
                                    } else {
                                        if (a % 12 == 0) {
                                            Calendar calendar = Calendar.getInstance();
                                            calendar.setTime(firstUpkeepTime);
                                            calendar.add(Calendar.MONTH, a);//增加12个月
                                            Date time = calendar.getTime();

                                            // 判断与当前时间差是否在 delayDay 之间 ，是的话状态4，不是的话状态5
                                            int status = 4;
                                            long daysBetweenTwoDate = CommonUtils.getDaysBetweenTwoDate(time, new Date());
                                            if (daysBetweenTwoDate > delayDay) {
                                                status = 5;
                                                isExistOverDueUpkeepFlag = true;
                                            }

                                            UpkeepHistory addUpkeepHistory = new UpkeepHistory();
                                            addUpkeepHistory.setAssetNumber(assetNumber);
                                            addUpkeepHistory.setCreateTime(new Date());
                                            addUpkeepHistory.setStatus(status);
                                            addUpkeepHistory.setDueUpkeepTime(time);
                                            addUpkeepHistory.setUpkeepType(upkeepType);
                                            upkeepHistoryDao.addUpkeepHistory(addUpkeepHistory);
                                        }
                                    }
                                }
                            }

                        }
                        if (isExistOverDueUpkeepFlag) {
                            pcbaDeviceDao.updateDeviceUpkeepStatus(assetNumber, 2);
                        } else {
                            pcbaDeviceDao.updateDeviceUpkeepStatus(assetNumber, 1);
                        }
                    } else {
                        //保养标志设置为正常 1
                        pcbaDeviceDao.updateDeviceUpkeepStatus(assetNumber, 1);
                    }
                }
            }

            // 根据设备资产获取所有设备 保养记录，只要有保养记录 不等于1且is_current = 1的，均需要把设别状态改为2
            for (PcbaDevice pcbaDevice : pcbaDeviceList) {
                String assetNumber = pcbaDevice.getAssetNumber();
                if (assetNumber != null && !"".equals(assetNumber)) {
                    /*Integer numByAssetNumber = upkeepHistoryDao.getNumByAssetNumber(assetNumber);
                    if(numByAssetNumber == 0){
                        pcbaDeviceDao.updateDeviceUpkeepStatus(assetNumber, 1);
                    }else{
                        Integer numByAssetNumber2 = upkeepHistoryDao.getNumByAssetNumber2(assetNumber);
                        if(numByAssetNumber2 != 0){
                            pcbaDeviceDao.updateDeviceUpkeepStatus(assetNumber, 2);
                        }
                    }*/
                    Integer numByAssetNumberOverdue = upkeepHistoryDao.getNumByAssetNumberOverdue(assetNumber);
                    if (numByAssetNumberOverdue > 0) {
                        pcbaDeviceDao.updateDeviceUpkeepStatus(assetNumber, 2);
                    } else {
                        pcbaDeviceDao.updateDeviceUpkeepStatus(assetNumber, 1);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
